home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17690 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  61 lines

  1. Path: news.lightlink.com!usenet
  2. From: jkramer@lightlink.com (Justin Kramer AKA Tinman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Computer hangs at the end of this when run. (iostreams and fstreams)
  5. Date: 17 Apr 1996 03:26:06 GMT
  6. Organization: Justin Kramer
  7. Message-ID: <4l1oce$18s@majesty.lightlink.com>
  8. NNTP-Posting-Host: port44.lightlink.com
  9. Mime-Version: 1.0
  10. X-Newsreader: WinVN 0.99.3
  11.  
  12. Hi,
  13.  
  14.     I JUST started to program in C++ with iostreams and I've started 
  15. off with an odd problem. When the following program is run, it hangs 
  16. after everything has executed.  When I took out the 'f << s;' it ran 
  17. without crashing. Here's the source:
  18.  
  19.  
  20. #include <iostream.h>
  21. #include <constrea.h>
  22. #include <fstream.h>
  23.  
  24. int main(void)
  25. {
  26.     conbuf    console;
  27.  
  28.     console.textcolor(7);
  29.     console.textbackground(0);
  30.     console.clrscr();
  31.  
  32.     cout << "Enter a line of text:\n";
  33.  
  34.     char *s;
  35.     cin.getline(s, 80);
  36.     char *fname;
  37.     cout << "Enter file name to store that line of text in: ";
  38.     cin.getline(fname, 79);
  39.  
  40.     ofstream f(fname);
  41.     if (!f)
  42.         cerr << "Couldn't open " << fname << "\n";
  43.     f << s;
  44.     f.close();
  45.  
  46.     cout << "\"" << s << "\"" << " written to file: " << fname << 
  47. "\n";
  48.  
  49.     cin.get();
  50.  
  51.     return 0;
  52.  
  53. }
  54.  
  55.     I'm using BC++ 3.1 f that matters.  Thanks in advance for any 
  56. help.
  57.  
  58.  
  59. -tin
  60.  
  61.